package com.itestor.iptv.modules.common.actions;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import com.itestor.iptv.global.actions.AbstractPagedQueryAction;
import com.itestor.iptv.global.bean.AbstractPagedQuery;
import com.itestor.iptv.modules.common.bean.RegionManageQuery;
import com.itestor.iptv.modules.common.domain.RegionManage;
import com.itestor.iptv.modules.common.service.RegionManageService;
import com.itestor.iptv.modules.common.utils.CommonConstant;

/**
 * 
 * @author Mr.li
 * 
 */
public class RegionManageQueryAction extends AbstractPagedQueryAction {

    /**
     * Logger for this class.
     */
    private static final Logger logger = Logger
            .getLogger(RegionManageQueryAction.class);

    private RegionManageService regionManageService;

    @SuppressWarnings("unchecked")
    @Override   
    public AbstractPagedQuery doPagedQuery(ActionForm form,
            HttpServletRequest request, int pagerCount, int pagerOffset)
            throws Exception {
        String regionId = request.getParameter("regionId");
        String subregionId = request.getParameter("subregionId");
        String regionName = request.getParameter("regionName");
        
        
        RegionManageQuery regionQuery = new RegionManageQuery();
        regionQuery.setPagerCount(pagerCount);
        regionQuery.setPagerOffset(pagerOffset);
        
        if (!StringUtils.isEmpty(regionId)) {
            regionQuery.setIdRegion(Integer.parseInt(regionId));
        }
        
        if (!StringUtils.isEmpty(subregionId)) {
            regionQuery.setIdRegion(Integer.parseInt(subregionId));
        }
        
        if (!StringUtils.isEmpty(regionName)) {
            regionQuery.setRegionName(regionName);
        }
        
        regionManageService.queryRegionManageList(regionQuery);        
        request.setAttribute(CommonConstant.REQUEST_ATTR_Q_REGION, regionQuery);
        
        List<RegionManage> regionList= regionManageService.getRootRegionList();
        request.setAttribute("RA_ROOT_REGIONS", regionList);
        
        Map<Integer,RegionManage> regionMap= regionManageService.getBelongToRegionMap();
        request.setAttribute("RA_REGION_MAP", regionMap);
        
        return regionQuery;
    }

    /**
     * 
     * @param pRegionManageService
     */
    public void setRegionManageService(RegionManageService pRegionManageService) {
        regionManageService = pRegionManageService;
    }

}